package cn.itcast.goods.service.impl;

import cn.itcast.context.BaseContext;
import cn.itcast.exception.DeletionNotAllowedException;
import cn.itcast.goods.dto.CategoryDTO;
import cn.itcast.goods.dto.CategoryPageQueryDTO;
import cn.itcast.goods.mapper.CategoryMapper;
import cn.itcast.goods.mapper.DishMapper;
import cn.itcast.goods.mapper.SetmealMapper;
import cn.itcast.goods.pojo.Category;
import cn.itcast.goods.pojo.Dish;
import cn.itcast.goods.pojo.Setmeal;
import cn.itcast.goods.service.CategoryService;
import cn.itcast.result.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 分类业务层
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分类分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageBean pageQuery(CategoryPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(dto);

        return PageBean.builder()
                .total(page.getTotal())
                .records(page.getResult())
                .build();
    }

    /**
     * 修改分类
     *
     * @param dto
     */
    @Override
    public void update(CategoryDTO dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);

        return;
    }

    /**
     * 启动、禁用分类
     *
     * @param status
     */
    @Override
    public void startOrStop(Long id, Integer status) {
        Category category = Category.builder()
                .createTime(LocalDateTime.now())
                .createUser(BaseContext.getCurrentId())
                .status(status)
                .id(id)
                .build();
        categoryMapper.update(category);
        return;
    }

    /**
     * 根据id删除分类
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) throws Exception {
//        - 步骤1：如果当前分类下关联了菜品，直接返回错误提示信息（抛出异常）
        List<Dish> dishs = dishMapper.getByCategoryId(id);
        if (dishs != null){
            throw new DeletionNotAllowedException("删除失败！当前分类下关联了菜品");
        }
//        - 步骤2：如果当前分类下关联了套餐，直接返回错误提示信息（抛出异常）
        List<Setmeal> setmeal = setmealMapper.getByCategoryId(id);
        if( setmeal != null){
            throw new DeletionNotAllowedException("删除失败！分类下关联了套餐");
        }
//        - 步骤3：删除分类数据
        categoryMapper.deleteById(id);

        return;
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> getByType(Integer type) {
        List<Category> categorys = categoryMapper.getByType(type);

        return categorys;
    }

    /**
     * 查询所有状态的分类
     *
     * @return
     */
    @Override
    public List<Category> getCategory() {
        List<Category> categorys = categoryMapper.getCategory();

        return categorys;
    }
}
